using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FutureOpen.Core;
using FutureOpen.Core.func;
using FutureOpen.Core.func.db;
using System.Collections.Generic;


public partial class ArtListByType : FutureOpen.Web.Pages.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.PageInit();
       
        if (!IsPostBack)
        {
            BindCatList();
            int catid = -1;
            try
            {
                catid = Convert.ToInt32(Request.QueryString["c"]);
            }
            catch
            {
                catid = -1;
            }

            if (catid == -1)
            {
                catid = Convert.ToInt32(ddlCatList.Items[0].Value);
            }
            else
            {
                for (int i = 0; i < ddlCatList.Items.Count; i++)
                {
                    if (ddlCatList.Items[i].Value.Equals(catid.ToString()))
                    {
                        ddlCatList.Items[i].Selected = true;
                    }
                }
            }
            AspNetPager1.RecordCount = GetArtsCount(catid);
            BindArts(catid, 0, 15);
        }
    }
    private void BindCatList()
    {
        MyCat mycat = new MyCat();
        IList<Cat> catList = mycat.GetChildren(CatType.CATTYPE_ROOT);

        ddlCatList.Items.Clear();
        ddlCatList.Items.Add(new ListItem("ȫѡ", "-1"));
        foreach (Cat cat in catList)
        {
            ListItem item = new ListItem(cat.Cat_name, cat.Cat_id.ToString());
            ddlCatList.Items.Add(item);
        }
    }
    private int GetArtsCount(int catid)
    {
        return new MyArt().GetCatArtCount(catid);
    }
    private void BindArts(int cat, int start, int count)
    {
        MyArt art = new MyArt();
        IList<Art> artList = null;
        if (cat == -1)
        {
            artList = art.GetArtByRange(start, count);
        }
        else
        {
            artList = art.GetCatArtByRange(cat, start, count);
        }

        DataList1.DataSource = artList;
        DataList1.DataBind();

    }
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        BindArts(Convert.ToInt32(ddlCatList.SelectedValue), (e.NewPageIndex - 1) * 15, 15);
    }
    protected void ddlCatList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("ArtListByType.aspx?c=" + ddlCatList.SelectedValue);
        //int catid = Convert.ToInt32(ddlCatList.SelectedValue);
        //AspNetPager1.RecordCount = GetArtsCount(catid);
        //BindArts(catid, 0, 15);
    }
}
